import Layer, { LyTip } from "../../common/cmpt/base/Layer";
import RecyclePool from "../../common/util/RecyclePool";
import { Tween } from "../../common/util/Tween";
import LyTipPre from "../other/LyTipPre";

const { ccclass, property } = cc._decorator;

export let lyTip = 'LyTip'



@ccclass
export default class DlgLyTip extends cc.Component {
    @property({ type: cc.Node, tooltip: '两个提示容器' }) tipGrp: cc.Node[] = []
    @property({ type: cc.Label, tooltip: '标题内容' }) titleLab: cc.Label = null
    @property({ type: cc.Node, tooltip: '控制标题移动节点' }) titleNode: cc.Node = null
    @property({ type: cc.Prefab, displayName: '提示框预制体' }) lyTipPre: cc.Prefab = null
    public static pUrl: string = 'LyDialog/DlgLyTip'

    tipData: LyTip = null
    canClickTitle: boolean = false
    titleTween: Tween<any> = null


    init(data: LyTip) {
        let tip = RecyclePool.get(lyTip)
        if (tip == null) {
            for (let i = 0; i < 10; i++) {
                RecyclePool.put(lyTip, cc.instantiate(this.lyTipPre))
            }
        } else {
            RecyclePool.put(lyTip, tip)
        }
        this.tipData = data
        this.canClickTitle = true
        this.titleInit()
        this.addTipGrp()
    }
    /**关闭当前页面以及提示页 */
    closeDlg() {
        Layer.inst.closeDialogs(this.tipData.url)
        for (let i = this.tipGrp[0].children.length; i > 0; i--) {
            RecyclePool.put(lyTip, this.tipGrp[0].children[i - 1])
        }
        for (let i = this.tipGrp[1].children.length; i > 0; i--) {
            RecyclePool.put(lyTip, this.tipGrp[1].children[i - 1])
        }
        Layer.inst.closeLyTip()
    }
    /**添加侧边提示 */
    addTipGrp() {
        if (this.tipData.hasOwnProperty("rightTip")) {
            for (let i = 0; i < this.tipData.rightTip.length; i++) {
                let item = RecyclePool.get(lyTip)
                item.getComponent(LyTipPre).init(true, `${this.tipData.rightTip[i]}`)
                item.anchorX = 1
                item.parent = this.tipGrp[1]
                item.setPosition(cc.v2(0))
            }
        }
        if (this.tipData.hasOwnProperty("leftTip")) {
            for (let i = 0; i < this.tipData.leftTip.length; i++) {
                let item = RecyclePool.get(lyTip)
                item.parent = this.tipGrp[0]
                item.getComponent(LyTipPre).init(false, `${this.tipData.leftTip[i]}`)
                item.setPosition(cc.v2(0))
            }
        }
        for (let i = 0; i < 2; i++) {
            this.tipGrp[i].getComponent(cc.Layout).updateLayout()
            this.tipGrp[i].getComponent(cc.Widget).updateAlignment()
        }
    }
    /**标题初始化 */
    titleInit() {
        this.titleLab.string = `${this.tipData.title}`
        this.titleNode.y = -470
        if (this.titleTween) {
            this.titleTween.stop()
        }
    }

    titleClick() {
        if (!this.canClickTitle) return
        this.canClickTitle = false
        if (this.titleNode.y == -470) {
            this.titleTween = new Tween(this.titleNode)
                .to({ y: -255 }, 0.3 * 1000)
                .onComplete(() => {
                    this.canClickTitle = true
                })
                .start()
        } else {
            this.titleTween = new Tween(this.titleNode)
                .to({ y: -470 }, 0.3 * 1000)
                .onComplete(() => {
                    this.canClickTitle = true
                })
                .start()
        }
    }
}
